package edu.xidian.sei.mlcs

import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ListBuffer

case class InNode(val loc: Location) {
  var indegree: Int = 0
  var level: Int = 0
  var count: BigDecimal = 0
  val predecessors = new ListBuffer[Location]()

  def reset(): InNode = {
    this.predecessors.clear()
    this.indegree = 0
    this.level = 0
    this
  }

  def inc {
    indegree = indegree + 1
  }

  def addCount(num: BigDecimal) {
    count = count + num
  }

  def addEndPredecessor(predecessor: Location, newLevel: Int): Unit = synchronized {
    if (this.level < newLevel) {
      this.level = newLevel
      predecessors.clear()
    }
    predecessors += predecessor
    indegree = indegree - 1
  }

  def addPredecessor(predecessor: Location, newLevel: Int): Boolean = synchronized {
    if (this.level < newLevel) {
      this.level = newLevel
      predecessors.clear()
    }
    predecessors += predecessor
    indegree = indegree - 1
    if (indegree < 0) println(loc + "indegree: " + indegree)
    indegree == 0
  }
  override def toString(): String = {
    loc + " g: " + indegree + " l: " + level + " p: " + predecessors
  }
}